<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseAdminController;
use Illuminate\Http\Request;
use App\Service\BasicService;
class BasicBatchController extends BaseAdminController {
    protected $basicService;

    public function _init(){
        $this->basicService = new BasicService();
    }

    public function index(Request $request){
        $search = $request->input();
        $data = $this->basicService->setModel('Admin.BasicBatch')->getBasicBatchList(isset($search['limit'])?$search['limit']:15,$search);
        $basic = $this->basicService->getBasicName($search['basic_id']);
        return view("admin.basic_batch.index",compact(["data","basic"]));
    }

    public function create(Request $request){
        if($request->isMethod("GET")){
            return view("admin.basic_batch.create");
        }else{
            $data = $request->except("_token");
            $this->validateRequest($request,"create");
            $data['exam_start_at'] = strtotime($data['exam_start_at']);
            $data['exam_end_at'] = strtotime($data['exam_end_at']);
            $result = $this->basicService->setModel('Admin.BasicBatch')->create($data);
            if($result > 0){
                return $this->success($result,"添加成功");
            }else{
                return $this->error(7000,"添加失败");
            }
        }
    }
    public function update(Request $request){
        if($request->isMethod("GET")){
            $id = $request->input('id');
            $data = $this->basicService->setModel('Admin.BasicBatch')->find($id);
            $data['exam_start_at'] = date("Y-m-d H:i:s",$data['exam_start_at']);
            $data['exam_end_at'] = date("Y-m-d H:i:s",$data['exam_end_at']);
            return view("admin.basic_batch.update",compact('data'));
        }else{
            $data = $request->except("_token");
            $this->validateRequest($request,"create");
            $data['exam_start_at'] = strtotime($data['exam_start_at']);
            $data['exam_end_at'] = strtotime($data['exam_end_at']);
            $result = $this->basicService->setModel('Admin.BasicBatch')->update($data['id'], $data);
            return $this->success($result, "编辑成功");
        }
    }
    public function destroy(Request $request) {
        $id = $request->input('id');
        $data = $this->basicService->setModel('Admin.BasicMember')->first(['batch_id'=>$id]);
        if($data){
            return $this->layuiDataError(7000,"请先移除考场内的人员");
        }
        $result = $this->basicService->deleteExam($id);
        return $this->success($result, "删除成功");
    }

}
